<template>
  <div>
    <el-form :inline="true">
      <el-form-item style="margin-left: 5px">
        <el-input
            v-model="searchForm.name"
            placeholder="请输入专利名称"
            clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getPatentList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="$router.push('/sys/application')">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-popconfirm title="这是确定批量删除吗？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column
          prop="patent.name"
          label="专利名称"
          width="120">
      </el-table-column>
      <el-table-column
          prop="patent.type"
          label="专利类型"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="patent.content"
          label="专利简介"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="helper.name"
          label="代理人"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="patent.createTime"
          label="创建时间"
          show-overflow-tooltip>
      </el-table-column>

      <el-table-column
          prop="patent.status"
          label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.patent.status === 0" type="info">已创建</el-tag>
          <el-tag size="small" v-else-if="scope.row.patent.status === 1" type="warning">评估中</el-tag>
          <el-tag size="small" v-else-if="scope.row.patent.status === 2">已评估</el-tag>
          <el-tag size="small" v-else-if="scope.row.patent.status === 3" type="danger">申请中</el-tag>
          <el-tag size="small" v-else-if="scope.row.patent.status === 4" type="danger">审核中</el-tag>
          <el-tag size="small" v-else-if="scope.row.patent.status === 5" type="success">已通过</el-tag>
          <el-tag size="small" v-else-if="scope.row.patent.status === 6" type="danger">在售</el-tag>
        </template>

      </el-table-column>
      <el-table-column
          prop="icon"
          label="操作">

        <template slot-scope="scope">
          <el-button type="text" @click="assessment(scope.row.patent.id,scope.row)"
                     v-if="scope.row.patent.status===0||scope.row.patent.status===2">开始评估
          </el-button>
          <el-divider direction="vertical" v-if="scope.row.patent.status===0||scope.row.patent.status===2"></el-divider>

          <el-button type="text" @click="apply(scope.row.patent.id,scope.row)"
                     v-if="scope.row.patent.status===0||scope.row.patent.status===2">立即申请
          </el-button>
          <el-divider direction="vertical" v-if="scope.row.patent.status===0||scope.row.patent.status===2"></el-divider>

          <el-button type="text" @click="revokeAssessment(scope.row.patent.id,scope.row)"
                     v-if="scope.row.patent.status===1">
            撤销评估
          </el-button>
          <el-divider direction="vertical" v-if="scope.row.patent.status===1"></el-divider>

          <el-button type="text" @click="revokeApply(scope.row.patent.id,scope.row)" v-if="scope.row.patent.status===3">
            撤销申请
          </el-button>
          <el-divider direction="vertical" v-if="scope.row.patent.status===3"></el-divider>

          <el-button type="text" @click="look(scope.row.patent.id,scope.row)">
            查看
          </el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="edit(scope.row.patent.id)"
                     v-if="scope.row.contact===true&&(scope.row.patent.status===0||scope.row.patent.status===2)">编辑
          </el-button>
          <el-divider direction="vertical"
                      v-if="scope.row.contact===true&&(scope.row.patent.status===0||scope.row.patent.status===2)"></el-divider>

          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.patent.id)"
                           v-if="scope.row.contact===true">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>

        </template>
      </el-table-column>

    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import moment from 'moment';

export default {
  name: "PatentStatus",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,

      total: 0,
      size: 10,
      current: 1,

      tableData: [],
      multipleSelection: [],
      status: '',
    }
  },
  created() {
    this.getPatentList()
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;

      this.delBtlStatu = val.length == 0
    },

    handleSizeChange(val) {
      this.size = val
      this.getPatentList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getPatentList()
    },

    getPatentList() {
      //根据名称查询或查询所有专利和其代理人
      this.$axios.get("/patent/getP&H", {
        params: {
          id: this.$store.state.userId,
          name: this.searchForm.name,
          current: this.current,
          size: this.size,
          status1:8,
          status2:9
        }
      }).then(res => {
        this.tableData = res.data.data;
        this.tableData.forEach(row => {
          row.patent.createTime = moment(row.patent.createTime).format('YYYY-MM-DD HH:mm');
        });
        this.total = this.tableData[0].total;
        console.log(res.data.data);
      })
    },
    apply(id, row) {
      this.status = 'apply';
      this.$axios.post("/patent/statusChange/" + this.status + "/" + id).then(res => {
        if (res.data.data === 1) {
          this.$message({
            message: '恭喜你，操作成功！',
            type: 'success'
          });
        }
        row.patent.status = 3;
      })
    },
    revokeApply(id, row) {
      this.status = 'revokeApply';
      this.$axios.post("/patent/statusChange/" + this.status + "/" + id).then(res => {
        if (res.data.data === 1) {
          this.$message({
            message: '恭喜你，操作成功！',
            type: 'success'
          });
        }
        row.patent.status = 2;
      })
    },
    edit(id) {
      this.$router.push({
        name: "sys:patent:application",
        params: {
          patentId: id
        }
      })
    },
    delHandle(id) {

      var ids = []

      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.patent.id)
        })
      }
      console.log("patentId:")
      console.log(ids);
      this.$axios.post("/patent/delete", ids).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getPatentList()
          }
        });
        console.log(res.data);
      })
    },
    assessment(id, row) {
      this.status = 'assessment';
      this.$axios.post("/patent/statusChange/" + this.status + "/" + id).then(res => {
        if (res.data.data === 1) {
          this.$message({
            message: '恭喜你，操作成功！',
            type: 'success'
          });
        }
        row.patent.status = 1;
      })
    },
    revokeAssessment(id, row) {
      this.status = 'revokeAssessment';
      this.$axios.post("/patent/statusChange/" + this.status + "/" + id).then(res => {
        if (res.data.data === 1) {
          this.$message({
            message: '恭喜你，操作成功！',
            type: 'success'
          });
        }
        row.patent.status = 0;
      })
    },
    look(id, row) {
      this.$router.push({
        name: "sys:patent:application",
        params: {
          patentId: id,
          disable: true,
        }
      })
    }
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}
</style>